<template>
  <div class="plugin-node-card" ref="card">
    <!-- 删除蓝点锚点相关代码 -->
    <div class="header">
      <span class="icon"></span>
      <span class="title">插件节点</span>
    </div>
    <div class="desc">调用第三方插件扩展功能</div>
    <div class="section-title">插件配置</div>
    <div class="config-list">
      <div class="config-row">
        <label>插件类型：</label>
        <el-select v-model="localConfig.pluginType" size="small" style="width: 100%;" @change="onPluginTypeChange;emitChange">
          <el-option label="文件处理" value="file" />
          <el-option label="图像处理" value="image" />
          <el-option label="文本处理" value="text" />
          <el-option label="数据分析" value="data" />
          <el-option label="自定义插件" value="custom" />
        </el-select>
      </div>
      <div class="config-row">
        <label>具体插件：</label>
        <el-select v-model="localConfig.pluginName" size="small" style="width: 100%;" placeholder="请选择插件" @change="emitChange">
          <el-option 
            v-for="plugin in availablePlugins" 
            :key="plugin.value" 
            :label="plugin.label" 
            :value="plugin.value" 
          />
        </el-select>
      </div>
      <div class="config-row">
        <label>版本：</label>
        <el-select v-model="localConfig.version" size="small" style="width: 100%;" placeholder="请选择版本" @change="emitChange">
          <el-option label="最新版本" value="latest" />
          <el-option label="稳定版本" value="stable" />
          <el-option label="指定版本" value="specific" />
        </el-select>
      </div>
    </div>
    <div class="section-title">插件参数</div>
    <div class="params-area">
      <div v-for="(param, idx) in localConfig.parameters" :key="idx" class="param-row">
        <el-input v-model="localConfig.parameters[idx].name" placeholder="参数名" size="small" style="width: 30%; margin-right: 8px;" @input="emitChange" />
        <el-input v-model="localConfig.parameters[idx].value" placeholder="参数值" size="small" style="width: 40%; margin-right: 8px;" @input="emitChange" />
        <el-select v-model="localConfig.parameters[idx].type" size="small" style="width: 20%; margin-right: 8px;" @change="emitChange">
          <el-option label="String" value="string" />
          <el-option label="Number" value="number" />
          <el-option label="Boolean" value="boolean" />
          <el-option label="Object" value="object" />
        </el-select>
        <el-button size="mini" type="danger" @click="removeParam(idx)">删除</el-button>
      </div>
      <el-button size="mini" type="primary" @click="addParam" style="margin-top: 8px;">添加参数</el-button>
    </div>
    <div class="section-title">执行配置</div>
    <div class="config-list">
      <div class="config-row">
        <label>超时时间：</label>
        <el-input-number v-model="localConfig.timeout" :min="1000" :max="300000" :step="1000" size="small" style="width: 100%;" @change="emitChange" />
        <span style="margin-left: 8px; color: #888;">毫秒</span>
      </div>
      <div class="config-row">
        <el-checkbox v-model="localConfig.retryOnError" @change="emitChange">失败重试</el-checkbox>
      </div>
      <div class="config-row" v-if="localConfig.retryOnError">
        <label>重试次数：</label>
        <el-input-number v-model="localConfig.retryCount" :min="1" :max="5" size="small" style="width: 100%;" @change="emitChange" />
      </div>
    </div>
    <div class="section-title">输出配置</div>
    <div class="config-list">
      <div class="config-row">
        <el-checkbox v-model="localConfig.saveToVariable" @change="emitChange">保存到变量</el-checkbox>
      </div>
      <div class="config-row" v-if="localConfig.saveToVariable">
        <label>变量名：</label>
        <el-input v-model="localConfig.outputVariable" size="small" placeholder="请输入输出变量名" @input="emitChange" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PluginNodeCard',
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      localConfig: { ...this.config },
      pluginOptions: {
        file: [],
        image: [],
        text: [],
        data: [],
        custom: []
      }
    }
  },
  watch: {
    config: {
      handler(newVal) {
        this.localConfig = { ...newVal }
      },
      deep: true
    }
  },
  computed: {
    availablePlugins() {
      // 保证 pluginOptions 存在且 pluginType 有值
      return (this.pluginOptions && this.localConfig && this.localConfig.pluginType && this.pluginOptions[this.localConfig.pluginType]) ? this.pluginOptions[this.localConfig.pluginType] : [];
    }
  },
  methods: {
    onPluginTypeChange() {
      this.localConfig.pluginName = '';
      this.localConfig.parameters = [];
    },
    addParam() {
      if (!Array.isArray(this.localConfig.parameters)) {
        this.$set(this.localConfig, 'parameters', []);
      }
      this.localConfig.parameters.push({ name: '', value: '', type: 'string' });
    },
    removeParam(idx) {
      this.localConfig.parameters.splice(idx, 1);
    },
    emitChange() {
      this.$emit('change', { ...this.localConfig })
    }
  }
};
</script>

<style scoped>
.plugin-node-card {
  width: 300px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.04);
  padding: 16px;
  position: relative;
}

.node-dot {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #2979ff;
  border: 2px solid #fff;
  box-shadow: 0 0 2px #2979ff;
  cursor: pointer;
  z-index: 10;
  transition: box-shadow 0.2s;
}

.node-dot--left-top {
  top: -6px;
  left: -6px;
}

.node-dot--right-top {
  top: -6px;
  right: -6px;
}

.node-dot:hover {
  box-shadow: 0 0 8px #2979ff;
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  position: relative;
}

.icon {
  width: 32px;
  height: 32px;
  background: #fff3e0;
  border-radius: 50%;
  display: inline-block;
  margin-right: 8px;
  position: relative;
}

.icon::after {
  content: '🔌';
  display: block;
  width: 16px;
  height: 16px;
  font-size: 16px;
  position: absolute;
  left: 8px;
  top: 8px;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.desc {
  color: #888;
  font-size: 13px;
  margin-bottom: 8px;
}

.section-title {
  font-weight: bold;
  margin-bottom: 8px;
  margin-top: 12px;
}

.section-title:first-of-type {
  margin-top: 0;
}

.config-list {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.config-row {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
}

.config-row:last-child {
  margin-bottom: 0;
}

.config-row label {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
  color: #333;
  width: 100%;
}

.params-area {
  background: #fafbfc;
  border-radius: 6px;
  padding: 12px;
}

.param-row {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style> 